﻿@using Orchard.ContentManagement
@using DreamStore.WebShop.Models
@using Orchard.Core.Title.Models

@{
    Style.Require("WebShop.ShoppingCart");
    Script.Require("WebShop.ShoppingCart");

    var items = (IList<dynamic>)Model.ShopItems;
    var subtotal = (decimal)Model.Subtotal;
    var vat = (decimal)Model.Vat;
    var total = (decimal)Model.Total;
}

@if (!items.Any())
{
    <p>You don't have any items in your shopping cart.</p>
    <a class="button" href="http://91.144.247.230/">Return To Home</a>
}
else {
    <div data-bind="visible: !hasItems()">
        <p>You don't have any items in your shopping cart.</p>

     
    </div>
    <div data-bind="visible: hasItems()">
        <article class="shoppingcart" data-load-shoppingcart-url="@Url.Action("GetItems", "ShoppingCart", new { area = "DreamStore.WebShop" })" data-update-shoppingcart-url="@Url.Action("Update", "ShoppingCart", new { area = "DreamStore.WebShop" })">
            @using(Html.BeginFormAntiForgeryPost(Url.Action("Update", "ShoppingCart", new { area = "DreamStore.WebShop" })))
            {
                <table>
                    <thead>
                        <tr>
                            <td>Article</td>
                            <td class="numeric">Unit Price</td>
                            <td class="numeric">Quantity</td>
                            <td class="numeric">Total Price</td>
                            <td class="action"></td>
                        </tr>
                    </thead>
                    <tbody data-bind='template: {name: "itemTemplate", foreach: items}'>
                        @for (var i = 0; i < items.Count; i++) {
                            var item = items[i];
                            var product = (ProductPart) item.Product;
                            var contentItem = (ContentItem) item.ContentItem;
                            var titlePart = contentItem.As<TitlePart>();
                            var title = titlePart != null ? titlePart.Title : "(no titlepart attached)";
                            var quantity = (int) item.Quantity;
                            var unitPrice = product.Price;
                            var totalPrice = quantity*unitPrice;
                            <tr>
                                <td>@title</td>
                                <td class="numeric">@unitPrice.ToString("c")</td>
                                <td class="numeric">
                                    <input name="@string.Format("items[{0}].ProductId", i)" type="hidden" value="@product.Id" />
                                    <input name="@string.Format("items[{0}].IsRemoved", i)" type="hidden" value="false" />
                                    <input name="@string.Format("items[{0}].Quantity", i)" type="number" value="@quantity" />
                                </td>
                                <td class="numeric">@totalPrice.ToString("c")</td>
                                <td class="action"><a class="icon delete postback" href="#"></a></td>
                            </tr>
                        }
            
                    </tbody>
                    <tfoot>
                        <tr><td colspan="5">&nbsp;</td></tr>
                        <tr class="separator">
                            <td class="update" colspan="5"><button name="command" value="Update" type="submit">Update</button></td>
                        </tr>
                        <tr>
                            <td class="numeric label" colspan="3">Subtotal:</td>
                            <td class="numeric"><span data-bind="text: Globalize.format(calculateSubtotal(), 'c')">@subtotal.ToString("c")</span></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td class="numeric label" colspan="3">VAT Represents (25%):</td>
                            <td class="numeric"><span data-bind="text: Globalize.format(calculateVat(), 'c')">@vat.ToString("c")</span></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td class="numeric label" colspan="3">Total:</td>
                            <td class="numeric"><span data-bind="text: Globalize.format(calculateTotal(), 'c')">@total.ToString("c")</span></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
                <footer>
                    <div class="group">
                        <div class="align left"><a class="button" href="http://91.144.247.230/">Continue shopping</a></div>
                        <div class="align right"><button type="submit" name="command" value="Checkout" id="proceedBut">Proceed to checkout</button></div>
                    </div>
                </footer>
            }
        </article>
        
        <script type="text/html" id="itemTemplate">
            <tr>
                <td><span data-bind="text: title"></span></td>
                <td class="numeric"><span data-bind="text: Globalize.format(unitPrice, 'c')"></span></td>
                <td class="numeric">
                    <input data-bind="attr: { name: 'items[' + index() + '].ProductId'}, value: id" type="hidden" />
                    <input data-bind="attr: { name: 'items[' + index() + '].Quantity'}, value: quantity" type="number" />
                </td>
                <td class="numeric"><span data-bind="text: Globalize.format(total(), 'c')"></span></td>
                <td><a class="icon delete" href="#"></a></td>
            </tr>

        </script>
    </div>
}